package com.ccit.employee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ccit.employee.mapper.MemberMapper;
import com.ccit.employee.service.MemberService;
import com.ccit.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Double getMonthMoneyByCoach(String name) {
        // 获取当前年月，例如：2024-04
        LocalDate now = LocalDate.now();
        // 获取本月第一天和最后一天
        YearMonth yearMonth = YearMonth.from(now);
        LocalDate startDate = yearMonth.atDay(1);
        LocalDate endDate = yearMonth.atEndOfMonth();

        // 转换为 LocalDateTime 表示当天的开始和结束时间
        LocalDateTime startDateTime = startDate.atStartOfDay();
        LocalDateTime endDateTime = endDate.atTime(LocalTime.MAX);

        // 构造查询条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("coach", name)
                .between("create_time", startDateTime, endDateTime);

        List<User> list = memberMapper.selectList(wrapper);
        double result = 0.0;
        result += list.stream()
                .mapToDouble(User::getPrice)
                .sum();
        return result;
    }
}
